package com.woniuxy.qiantai.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.qiantai.entity.Comment;
import com.woniuxy.qiantai.entity.Order;
import com.woniuxy.qiantai.entity.User;
import com.woniuxy.qiantai.service.CommentService;
import com.woniuxy.qiantai.service.OrderService;
import com.woniuxy.qiantai.service.ShopService;
import com.woniuxy.qiantai.service.UserService;
import com.woniuxy.qiantai.utils.CookieUtils;
import com.woniuxy.qiantai.utils.JwtUtils;
import com.woniuxy.qiantai.vo.OrderItemVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author firstGroup
 * @since 2023-02-22 09:45:58
 */
@Controller
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    CommentService commentService;

    @Autowired
    UserService userService;
    @Autowired
    ShopService shopService;
    @Autowired
    OrderService orderService;

    @RequestMapping("commentInfo")
    @ResponseBody
    public Page<Comment> getComment(Integer currentPage, Integer pageSize, Integer shopId, HttpServletResponse resp){

        Page<Comment> commentList = commentService.selectCommentByshopId(currentPage,pageSize,shopId);
        return commentList;
    }
    @RequestMapping("toComment")
    public String comment(Integer orderId,Model model){
        List<OrderItemVo> orderItemVos = commentService.selectCommentByOrderId(orderId);
        String shopName = shopService.getShopName(orderId);
        model.addAttribute("orderId",orderId);
        model.addAttribute("shopName",shopName);
        model.addAttribute("orderItemList",orderItemVos);
        return "commentAndRate";
    }

    @RequestMapping("/grade")
    @ResponseBody
    public double selectAll(Integer shopId){

        List<Comment> comments = commentService.queryComments(shopId);
        System.out.println(comments);
        System.out.println(comments.size());
        if(comments != null && comments.size()!=0){
            Double grade = new Double(0.00);
            for (Comment comment : comments) {
                grade = grade + comment.getGrade();
                System.out.println(grade);
            }
            Double grades = grade/comments.size();
            DecimalFormat df = new DecimalFormat("#.0");
            String format = df.format(grades);
            Double gra = Double.parseDouble(format);
            System.out.println(gra);
            return gra;
        }else {
            return 4.0;
        }
    }
    public Integer currentUserName(HttpServletRequest request) {

        String userTokenFromCookie = CookieUtils.getUserTokenFromCookie(request);
        //然后从userToken中解析出用户名
        String accountFromToken = JwtUtils.getAccountWithoutException(userTokenFromCookie);
        User userByUserName = userService.getUserByUserName(accountFromToken);
        Integer id = userByUserName.getId();
        return id;

    }
    @RequestMapping("insertComment")
    @ResponseBody
    public String insertComment(HttpServletRequest request,String comment,Double rate,Integer orderId){
        Order order = orderService.selectOrderInfo(orderId);
        Integer userId = currentUserName(request);
        int result = commentService.insertCommentAndRate(comment, rate,orderId,order.getShopId(),userId);
        if (result == 1){
            orderService.updateOrderState(orderId,7);
            return "ok";
        }else {
            return "fail";
        }

    }

}

